package org.shopen.ai.puremvc.view.project
{
	import flash.events.Event;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	import org.shopen.ai.puremvc.ApplicationConstants;
	import org.shopen.ai.puremvc.model.settings.ICommitSettingsResult;
	import org.shopen.ai.puremvc.model.settings.SettingsProxy;
	import org.shopen.ai.puremvc.model.settings.SettingsType;
	import org.shopen.ai.puremvc.view.project.components.ProjectSetupView;
	import org.shopen.ai.puremvc.view.project.events.SetupViewIndexEvent;

	public class ProjectSetupViewMediator extends Mediator
	{
		public static const NAME : String = "ProjectSetupMediator";
		
		protected function get projectSetupView() : ProjectSetupView
		{
			var result : ProjectSetupView = viewComponent as ProjectSetupView;
			return result;
		} 
		
		public function ProjectSetupViewMediator(viewComponent:ProjectSetupView=null)
		{
			super(ProjectSetupViewMediator.NAME, viewComponent);
		}
		
		override public function getMediatorName():String
		{
			return ProjectSetupViewMediator.NAME;
		}
		
		override public function onRegister():void
		{
			// TODO: register State Selection Mediator
			if (projectSetupView.manualSetupViewCreated)
			{
				registerManualViewMediator();
			}
			if (projectSetupView.automaticSetupViewCreated)
			{
				registerAutomaticViewMediator();
			}
			addEventListeners();
		}
		
		protected function addEventListeners() : void
		{
			projectSetupView.addEventListener(ProjectSetupView.MANUALSETUPVIEW_CREATED, onManualSetupViewCreated);
			projectSetupView.addEventListener(ProjectSetupView.AUTOMATICSETUPVIEW_CREATED, onAutomaticSetupViewCreated);
			projectSetupView.addEventListener(SetupViewIndexEvent.SETUPVIEW_INDEX_CHANGED, onSetupViewChanged);
		}
		
		private function onManualSetupViewCreated(event : Event) : void
		{
			registerManualViewMediator(); 
		}
		
		private function onAutomaticSetupViewCreated(event : Event) : void
		{
			 registerAutomaticViewMediator();
		}
		
		private function onSetupViewChanged(event : SetupViewIndexEvent) : void
		{
			var type : SettingsType = SettingsType.MANUAL;
			switch (event.index)
			{
				case ProjectSetupView.MANUAL:
					type = SettingsType.MANUAL;
					break;
				case ProjectSetupView.AUTOMATIC:
					type = SettingsType.AUTOMATIC;
					break;
			}
			sendNotification(ApplicationConstants.CHANGE_SETTINGS_TYPE, type);
		}
		
		protected function registerManualViewMediator() : void
		{
			if (!facade.hasMediator(ManualSetupViewMediator.NAME))
			{
				facade.registerMediator(new ManualSetupViewMediator(projectSetupView.manualSetupView));
			}
		}
		
		protected function registerAutomaticViewMediator() : void
		{
			if (!facade.hasMediator(AutomaticSetupViewMediator.NAME))
			{
				facade.registerMediator(new AutomaticSetupViewMediator(projectSetupView.automaticSetupView));
			}
		}
		
		override public function listNotificationInterests():Array
		{
			// TODO: implement
			return [SettingsProxy.COMMIT_AUTOSETTINGS_RESULT,
					SettingsProxy.COMMIT_MANUALSETTINGS_RESULT];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			var name : String = notification.getName();
			switch (name)
			{
				case SettingsProxy.COMMIT_AUTOSETTINGS_RESULT:
				case SettingsProxy.COMMIT_MANUALSETTINGS_RESULT:
					var result : ICommitSettingsResult = notification.getBody() as ICommitSettingsResult;
					// TODO: implement disabling/enabling process button
					break;
			}
		}
	}
}